package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;

    @Override
    public PageResult page(DishPageQueryDTO queryDTO) {
        log.info("分页查询菜品信息 {}", queryDTO);
        //配置分页参数
        PageHelper.startPage(queryDTO.getPage(), queryDTO.getPageSize());
        //查询分页数据
        Page<Dish> page = dishMapper.page(queryDTO);
        PageInfo<Dish> dishPageInfo = new PageInfo<>(page);
        long total = dishPageInfo.getTotal();
        List<Dish> records = dishPageInfo.getList();
        return new PageResult(total, records);
    }

    /*
     * 新增菜品信息及其配料信息
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveWithFlavor(DishDTO dishDTO) {
        log.info("新增菜品信息及其配料信息 {}", dishDTO);
        //向菜品表插入一条数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
        //获取菜品id
        Long id = dish.getId();
        //向口味表插入n条数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(id));
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    //根据id查询菜品数据及其配料信息


    @Override
    public DishVO selectByidWithFlavor(Long id) {

        DishVO dishVO = dishMapper.selectByidWithFlavor(id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id);
        dishVO.setFlavors(dishFlavors);
        log.info("查询菜品信息及其配料信息 {}", dishVO);
        return dishVO;
    }

    //根据分类id查询菜品列表
    @Override
    public List<Dish> list(Long categoryId) {
        Dish build = new Dish().builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();

        List<Dish> dishes = dishMapper.list(build);
        log.info("根据分类id查询菜品列表 {}", dishes);
        return dishes;
    }

    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    /**
     * 更新菜品信息
     */
    @Override
    public void updata(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //修改菜品表基本信息
        dishMapper.update(dish);
        //删除原有的口味数据
        dishFlavorMapper.deleteByDishId(dishDTO.getId());

        //重新插入口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            //向口味表插入n条数据
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    /*
     * 删除菜品信息批量删除
     */

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<Integer> ids) {
        log.info("删除菜品信息 {}", ids);
        for (Integer id : ids) {
            //判断当前菜品是否在售卖中，如果在售卖中，则不能删除
            Dish dish = dishMapper.selectByid(id);
            if (dish.getStatus() == 1) {
                throw new RuntimeException("当前菜品正在售卖中，不能删除");
            }
        }
        //判断当前菜品是非被套餐引用，如果被引用，则不能删除
        List<Integer> setMealIds = setMealDishMapper.getDishListBySetMealId(ids);
        if (!setMealIds.isEmpty()) {
            throw new RuntimeException("当前菜品已被套餐引用，不能删除");
        }
        //删除菜品信息及其配料信息
        dishMapper.deleteBatch(ids);

        //删除菜品关联的口味数据
        dishFlavorMapper.deleteBatchFlavor(ids);
    }
}
